var DateUtil = function () {
    var dateUtil = new Object();
    function getYMD(date){
        var year = date.getFullYear(),
            month = date.getMonth()+1,
            day = date.getDate();
        return {"y":year,"m":month,"d":day};
    }
    function getQuarter(date){
        var month = date.getMonth();
        if(0<=month && 2 >= month){
            return 0;
        }else if(3<=month && 5 >= month){
            return 3;
        }else if(6<=month && 8 >= month){
            return 6;
        }else if(9<=month && 11 >= month){
            return 9;
        }
        return 0;
    }
    function formatYMD(ymd,separator){
        var format = ""
        for(arg in ymd){
            ymd[arg] = ("00" + ymd[arg]).substr(("" + ymd[arg]).length>=2?2:("" + ymd[arg]).length);
            format = format+ymd[arg]+separator;
        }
        return format.substring(0,format.length-1)

    }
    dateUtil.formatDate = function(date,fmt,isShowWeek){
        var week = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒

        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        if(isShowWeek){
            fmt = week[date.getDay()]+" "+fmt
        }
        return fmt;
    }
    dateUtil.getToday = function(){
        formatYMD({"y":1111,"m":11,"d":1},"-")
        var date = new Date;
        var ymd = getYMD(date);
        return {"o":formatYMD(ymd,"-"),"t":formatYMD(ymd,"-")}
    }
    dateUtil.getYesterday = function(){
        var date = new Date;
        date.setDate(date.getDate()-1);
        var ymd = getYMD(date);
        return {"o":formatYMD(ymd,"-"),"t":formatYMD(ymd,"-")}
    }
    dateUtil.getThisWeek = function(){
        var date = new Date;
        var week = date.getDay() == 0 ? 7 : date.getDay();
        date.setDate(date.getDate()-(week-1));
        var symd = getYMD(date);
        date.setDate(date.getDate()+6);
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getLastWeek = function(){
        var date = new Date;
        var week = date.getDay() == 0 ? 7 : date.getDay();
        date.setDate(date.getDate()-(week-1)-7);
        var symd = getYMD(date);
        date.setDate(date.getDate()+6);
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getThisMonth = function(){
        var date = new Date;
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(date.getMonth()+1)
        date.setDate(date.getDate()-1)
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getLastMonth = function(){
        var date = new Date;
        date.setMonth(date.getMonth()-1)
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(date.getMonth()+1)
        date.setDate(date.getDate()-1)
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getThisQuarter = function(){
        var date = new Date;
        var fq = getQuarter(date);
        date.setMonth(fq);
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(date.getMonth()+3)
        date.setDate(date.getDate()-1)
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getLastQuarter = function(){
        var date = new Date;
        var fq = getQuarter(date);
        date.setMonth(fq - 3);
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(date.getMonth()+3)
        date.setDate(date.getDate()-1)
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.getThisYear = function(){
        var date = new Date;
        date.setMonth(0);
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(11)
        date.setDate(31)
        var eymd = getYMD(date);
        return {"o":formatYMD(symd,"-"),"t":formatYMD(eymd,"-")}
    }
    dateUtil.init = function(){
        var date = new Date;
        date.setMonth(1);
        date.setDate(1);
        var symd = getYMD(date);
        date.setMonth(12)
        date.setDate(31)
        var eymd = getYMD(date);
        return {"o":"","t":""}
    }
    dateUtil.getDays = function(date){
        return date.getDate();
    }
    dateUtil.isEnableDay = function(year,month,day){
        var rdate = new Date(year,month,day,23,59,59);
        var cdate = new Date();
        if(year > cdate.getFullYear()|| month >cdate.getMonth()){
            cdate = new Date(year,month,1);
        }
        return rdate.getTime() > cdate.getTime() ? (rdate.getMonth() == cdate.getMonth()) : false;
    }
    dateUtil.getDay = function(year,month,day){
        var date = new Date(year,month,day);
        return {"a":dateUtil.formatDate(date,'yyyy-MM-dd'),"b":date.getDate()};
    }
    dateUtil.getMonthFirstDayWeek = function(year,month){
        return (new Date(year,month,1)).getDay();
    }
    dateUtil.getRegionTime = function(startTime,endTime){
        var timeDate = "";
        if ((startTime != null && startTime != "")||(endTime != null && endTime!= "")) {
            if(startTime == null || startTime == ""){
                timeDate += "至"+dateUtil.formatDate(new Date(endTime), "yyyy-MM-dd");
            }else if(endTime == null || endTime == ""){
                timeDate += dateUtil.formatDate(new Date(startTime), "yyyy-MM-dd")+"至"+dateUtil.formatDate(new Date(), "yyyy-MM-dd");
            }else{
                if(dateUtil.formatDate(new Date(startTime), "yyyy-MM-dd") == dateUtil.formatDate(new Date(endTime), "yyyy-MM-dd")){
                    timeDate += dateUtil.formatDate(new Date(startTime), "yyyy-MM-dd");
                }else{
                    timeDate += dateUtil.formatDate(new Date(startTime), "yyyy-MM-dd")+"至"+dateUtil.formatDate(new Date(endTime), "yyyy-MM-dd");
                }
            }

        }else{
            timeDate += "至"+dateUtil.formatDate(new Date(), "yyyy-MM-dd");
        }
        return timeDate;
    }
    return dateUtil;
}
var FillTable = function(){
    var fillTable = new Object();
    fillTable.tb = null;
    var getTable = function(csTb){
        return $('<table class="'+csTb.clz+'" style="'+csTb.css+'"></table>');
    }
    var getTh = function(cols,csTr,csTh){
        var th = '<thead><tr class="'+csTr.clz+'" style="'+csTr.css+'">';
        for(var i = 0;i < cols.length;i++){
            th += '<th class="'+csTh.clz+'" style="'+csTh.css+'">'+cols[i]["title"]+'</th>';
            if(i == 0){
                cols[i]["sum"] = "合计"
            }else if(cols[i]["isSum"]){
                cols[i]["sum"] = 0;
            }else{
                cols[i]["sum"] = "";
            }
        }
        th += '</tr></thead>';
        return $(th);
    }
    var getTd = function(cols,data,csTr,csTd){
        var td = '<tbody>';
        for(var i = 0;i < data.length;i++){
            td += '<tr class="'+csTr.clz+'" style="'+csTr.css+'">';
            for(var j = 0;j < cols.length;j++){
                var css = csTd.css;
                if(cols[j]["width"]){
                    css += ";width:"+cols[j]["width"];
                }
                td += '<td class="'+csTd.clz+'" style="'+css+'">'+(data[i][cols[j]["field"]] || "")+'</td>';
            }
            td += '</tr>';
        }
        td += '</tbody>';
        return $(td);
    }
    var getHeader = function(cp){
        var tpl = cp.tpl,cnt = cp.cnt;
        var caption = '<caption>';
        if(tpl == 1){
            caption += '<div class="'+cnt[0]["clz"]+'" style="'+cnt[0]["css"]+'">'+cnt[0]["title"]+'</div>';
            caption += '<div class="'+cnt[1]["clz"]+'" style="'+cnt[1]["css"]+'">'+cnt[1]["title"]+'</div>';
            caption += '<div class="'+cnt[2]["clz"]+'" style="'+cnt[2]["css"]+'">'+cnt[2]["title"]+'</div>';
        }else if(tpl == 2){
            caption += '<div class="'+cnt[0]["clz"]+'" style="'+cnt[0]["css"]+'">'+cnt[0]["title"]+'</div>';
            caption += '<div class="'+cnt[1]["clz"]+'" style="'+cnt[1]["css"]+'">'+cnt[1]["title"]+'</div>';
            caption += '<div class="'+cnt[2]["clz"]+'" style="'+cnt[2]["css"]+'">'+cnt[2]["title"]+'</div>';
            caption += '<div class="'+cnt[3]["clz"]+'" style="'+cnt[3]["css"]+'">'+cnt[3]["title"]+'</div>';
        }else if(tpl == 3){
            caption += '<div style="width:100%;">';
            caption += '<div style="'+cnt[0]["css"]+'">'+cnt[0]["title"]+'</div>';
            caption += '<div style="'+cnt[1]["css"]+'">'+cnt[1]["title"]+'</div>';
            caption += '<div style="'+cnt[2]["css"]+'">'+cnt[2]["title"]+'</div></div>';
        }
        caption += '</caption>';
        return $(caption);
    }
    var getTotalRow = function(cols,data,csTr,csTd){
        for(var i = 0;i < data.length;i++){
            if(data[i]["ROWNUM_"] == undefined)
                continue;
            for(var j = 1;j < cols.length;j++){
                if(cols[j]["isSum"]){
                    cols[j]["sum"] += parseFloat(data[i][cols[j]["field"]]);
                }
            }
        }
        var td = '<tr class="trow '+csTr.clz+'" style="'+csTr.css+'">';
        for(var j = 0;j < cols.length;j++){
            var sum = cols[j]["isSum"] ? cols[j]["sum"].toFixed(2):cols[j]["sum"];
            td += '<td class="'+csTd.clz+'" style="'+csTd.css+'">'+sum+'</td>';
        }
        td += '</tr>';
        return $(td);
    }
    var getFooter = function(fr){
        var tpl = fr.tpl,cnt = fr.cnt;
        var footer ='<footer>';
        if(tpl == 1){
            footer += '<div style="margin-top:10px;clear:both;font-size:18px;padding:8px 15px;"><span style="float:right;margin-right:8px;">复核人：_______________</span><span style="float:right;margin-right:25px;">盘点人：_______________</span></div>'
        }
        footer += '</footer>'
        return $(footer);
    }
    fillTable.ft = function(cols,data,css,cp,cf,fr){
        var tb = getTable(css["csTb"]);
        var th = getTh(cols,css["csTr"],css["csTd"]);
        var td = getTd(cols,data,css["csTr"],css["csTd"]);
        if(cp != "" && cp != undefined && cp != null){
            var header = getHeader(cp);
            tb.append(header);
        }
        if(cf != "" && cf != undefined && cf != null){
            var totalRow = getTotalRow(cols,data,css["csTr"],css["csTd"]);
            td.append(totalRow);
        }
        tb.append(th).append(td);
        if(fr != "" && fr != undefined && fr != null){
            var footer = getFooter(fr);
            tb = $('<div></div>').append(tb).append(footer);
        }
        fillTable.tb = tb;
    }
    return fillTable;
}

var cookie = {
    set : function(k,v,d){
        var date = new Date();
        d = d ? d : 7;
        date.setTime(date.getTime()+d*24*3600*1000)
        document.cookie = k + "=" + v +";expires="+date.toGMTString();
    },
    get : function(k){
        if(k){
            var cstr = document.cookie.replace(/[ ]/g,"");
            var carr = cstr.split(";");
            for(var i = 0;i < carr.length;i++){
                var ckv = carr[i].split("=");
                if(k == ckv[0])
                    return ckv[1];
            }
            return "";
        }
        return "";
    },
    del : function(k){
        var date = new Date();
        date.setTime(date.getTime() - 1000);
        document.cookie = k + "=v;expires="+date.toGMTString();
    }
}